//
//  SYResourcesTableViewCell.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/23.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYResourcesTableViewCell.h"
#import "SYShiXunModel.h"
#import "SYResourcesHeaderView.h"
#import "SYResourcesDetailsTableViewCell.h"

@interface SYResourcesTableViewCell () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *curTableView;

@property (strong, nonatomic) NSMutableArray *dataArr;



@end

@implementation SYResourcesTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
        [self initSubView];
    }
    return self;
}
//传入数据
- (void)lncomingData:(NSMutableArray *)listArr indexPath:(NSIndexPath *)indexPath{
    _dataArr = listArr;
    
    [self.curTableView reloadData];
}


- (void)initSubView {
    //表
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self).offset(14);
        make.right.equalTo(self.mas_right).offset(-14);
        make.bottom.equalTo(self.mas_bottom).offset(0);
    }];
}

#pragma mark - tab
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    SYZiYuanShenQingModel *zyModel = self.dataArr[section];
    if (zyModel.isSelect == YES) {
        return zyModel.listArr.count;
    } else {
        return 0;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYResourcesDetailsTableViewCell *detailsCell = [tableView dequeueReusableCellWithIdentifier:@"SYResourcesDetailsTableViewCell" forIndexPath:indexPath];
    detailsCell.selectionStyle = UITableViewCellSelectionStyleNone;
    [detailsCell resourcesDetailsTableView:indexPath];
    
    return detailsCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [UIView new];
    
    SYResourcesHeaderView *headerView = [[NSBundle mainBundle] loadNibNamed:@"SYResourcesHeaderView" owner:nil options:nil].firstObject;
    [view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(view);
        make.left.equalTo(view.mas_left).offset(14);
        make.right.equalTo(view.mas_right).offset(-14);
    }];
    [headerView resourcesHeaderViewIndexPath:section ziyuanmodel:self.dataArr[section]];
    
    __weak typeof(self) weakSelf = self;
    headerView.block = ^(SYZiYuanShenQingModel *zyModel, SYResourcesHeaderType type) {
        if (type == SYResourcesHeaderTypeDetails) {
            zyModel.isSelect =!zyModel.isSelect;
            [weakSelf.curTableView reloadData];
        } else {//学习心得
            if ([weakSelf.delegate respondsToSelector:@selector(resourcesTableView:)]) {
                [self.delegate resourcesTableView:SYDetailsTypeZiYuanXind];
            }
        }
        
    };
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}


#pragma mark - set
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _curTableView.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //[_curTableView registerCellWithClassNames:@[@"SYChuangTableViewCell",@"SYAddMembersTableViewCell"]];
        [_curTableView registerCellWithNIbNames:@[@"SYResourcesDetailsTableViewCell"]];
    }
    return _curTableView;
}
@end
